// ----------------------------------------------------------------------------//
// ExceptionAppender.java
// Created by user On 2012-10-23
package com.demo.framework.log;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

import android.util.Log;

public class ErrorTableAppender extends AppenderSkeleton {
	final static private String DEFAULT_NAME = "ErrorTableAppender";
	final static private String PACKAGE_PREFIX = "jitmarketing";
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");

	public ErrorTableAppender() {
		super.setName(DEFAULT_NAME);
		super.addFilter(new Filter() {
			@Override
			public int decide(LoggingEvent event) {
				return event.getLevel().isGreaterOrEqual(Level.ERROR) ? ACCEPT : DENY;
			}
		});
	}

	@Override
	public void close() {
		// DO NOTHING
	}

	@Override
	public boolean requiresLayout() {
		return false;
	}

	@Override
	protected void append(LoggingEvent event) {
		Throwable t = event.getThrowableInformation() == null ? null : event.getThrowableInformation().getThrowable();
		String timestamp = format.format(new Date(event.getTimeStamp()));
		Log.e("ERROR", new ExceptionLogInfo(event.getMessage().toString(), t, timestamp, PACKAGE_PREFIX).toString());
	}
}

// ----------------------------------END---------------------------------------//